package com.lancoo.ccas52.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lancoo.ccas52.entity.CollegeDealTime;
import com.lancoo.ccas52.mapper.CollegeDealTimeMapper;
import com.lancoo.ccas52.pojo.vo.DealTimeInput;
import com.lancoo.ccas52.service.CollegeDealTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas52.service.YunBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 院系管理员各个流程处理时间段 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2022-12-30
 */
@Service
public class CollegeDealTimeServiceImpl extends ServiceImpl<CollegeDealTimeMapper, CollegeDealTime> implements CollegeDealTimeService {

    @Resource
    private YunBasicService yunBasicService;

    @Autowired
    private HttpServletRequest request;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CollegeDealTime addOrUpdateDealTime(DealTimeInput dealTimeInput) {
        CollegeDealTime collegeDealTime = new CollegeDealTime();
        BeanUtil.copyProperties(dealTimeInput, collegeDealTime);
        //为null则新增  否则更新
        CollegeDealTime dealTime = this.getDealTime(dealTimeInput.getTaskId(), dealTimeInput.getType());
        if (ObjUtil.isNull(dealTime)){
            this.save(collegeDealTime);
            // 发送消息
            this.sendOpenMag(collegeDealTime);
        }else {
            collegeDealTime.setId(dealTime.getId());
            this.updateById(collegeDealTime);
        }
        return collegeDealTime;
    }

    public void sendOpenMag(CollegeDealTime collegeDealTime){
        Integer taskId = Integer.parseInt(request.getHeader("taskId"));
        String userId = request.getHeader("userId");
        String userName = request.getHeader("userName");
        switch (collegeDealTime.getType()) {
            case 1:
                if (collegeDealTime.getIsEnable() == 1) {
                    yunBasicService.sendOpenCoursePlanApplyMsg(collegeDealTime.getSetBeginTime(), collegeDealTime.getSetEndTime(), taskId, userId, userName);
                }
                break;
            case 3:
                if (collegeDealTime.getIsEnable() == 1) {
                    yunBasicService.sendOpenTeachTaskApplyMsg(collegeDealTime.getSetBeginTime(), collegeDealTime.getSetEndTime(), taskId, userId, userName);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public CollegeDealTime getDealTime(Integer taskId, Integer type) {
        CollegeDealTime collegeDealTime = this.getOne(new LambdaQueryWrapper<CollegeDealTime>()
                .eq(CollegeDealTime::getTaskId, taskId)
                .eq(CollegeDealTime::getType, type), false);
        return collegeDealTime;
    }


}
